package com.li.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.li.views.RecycleImageView;


/**
 * 缓存类
 * Created by Li on 2016/7/7.
 */
public class ViewHolder {

    //该类存储K-M,但只支持<Integer,Object> 效率比HashMap高
    private SparseArray<View> views;
    private View convertView;
    private Context context;

    public ViewHolder(Context context, int layoutId, int position, ViewGroup parent) {
        views = new SparseArray();
        this.context = context;
        convertView = LayoutInflater.from(context).inflate(layoutId,parent,false);
        convertView.setTag(this);
    }
    public static ViewHolder get(Context context,int layoutId, View convertView,
                                  int position,ViewGroup parent){
        if(convertView == null){
            ViewHolder holder = new ViewHolder(context,layoutId,position,parent);
            return holder;
        }else {

            return (ViewHolder) convertView.getTag();
        }
    }

    /**
     * 通过viewId获取控件,没有则添加
     * @param viewId 控件id
     * @param <T>   控件类型泛型
     * @return  控件
     */
    public <T extends View> T getView(int viewId){
        View view = views.get(viewId);
        if(view == null){
            view = convertView.findViewById(viewId);
            views.put(viewId, view);
        }
        return (T) view;
    }

    public View getConvertView(){
        return convertView;
    }

    public ViewHolder setText(int viewId, String str){
        TextView tv = getView(viewId);
        tv.setText(str);
        return this;
    }
    public ViewHolder setImageResource(int viewId, int resId){
        ImageView iv = getView(viewId);
        iv.setImageResource(resId);
        return this;
    }

    public ViewHolder setRating(int viewId, int num){
        RatingBar ratingBar = getView(viewId);
        ratingBar.setRating(num);
        ratingBar.setNumStars(num);
        return this;
    }
    public ViewHolder setImg(int viewId, String picPath){

        ImageView iv = getView(viewId);
        iv.measure(0,0);
        if (picPath != null && !picPath.equals("")){
            Glide.with(context).load(picPath)
//            .resize(iv.getMeasuredWidth(),iv.getMeasuredHeight())
                    .into(iv);
        }

        return this;
    }


    public ViewHolder setImageNormal(int viewId, String picUrl){
        ImageView imageView = getView(viewId);
        imageView.measure(0, 0);
        if (!TextUtils.isEmpty(picUrl)){
            Glide.with(context)
                    .load(picUrl)
                    .into(imageView);

        }

        return this;
    }
    public ViewHolder setImageLarge(int viewId, String picUrl){
        RecycleImageView imageView = getView(viewId);
        imageView.measure(0, 0);
        Glide.with(context)
                .load(picUrl)
                .into(imageView);
        return  this;
    }

}
